// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RollingAverageTest.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Numerics
{
    using System;

    using Microsoft.Robotics.Numerics;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Test class for RollingAverage"/>
    /// </summary>
    [TestClass]
    public class RollingAverageTests
    {
        /// <summary>
        /// Construct with invalid type test
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void RollingAverageInvalidFormat()
        {
            RollingAverage<string> ra = new RollingAverage<string>(10);
            try
            {
                ra.AddSample("hello");
            }
            catch (FormatException)
            {
                return;
            }

            Assert.Fail("Expected FormatException but never got it");
        }

        /// <summary>
        /// Construct with valid type test
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void RollingAverageConstructor()
        {
            RollingAverage<int> intAverage = new RollingAverage<int>(10);
            Assert.AreEqual(0, intAverage.Average);
            Assert.AreEqual(0, intAverage.SampleCount);
        }

        /// <summary>
        /// average with no samples
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void RollingAverageWithNoSamples()
        {
            RollingAverage<double> avg = new RollingAverage<double>(10);
            Assert.AreEqual(0, avg.Average);
        }

        /// <summary>
        /// average with partial samples
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void RollingAverageWithPartialSamples()
        {
            RollingAverage<double> avg = new RollingAverage<double>(10);
            avg.AddSample(10);
            avg.AddSample(20);
            Assert.AreEqual(15, avg.Average);
        }

        /// <summary>
        /// average with full samples
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void RollingAverageWithFullSamples()
        {
            RollingAverage<double> avg = new RollingAverage<double>(5);
            avg.AddSample(1);
            avg.AddSample(2);
            avg.AddSample(3);
            avg.AddSample(4);
            avg.AddSample(5);
            Assert.AreEqual(3, avg.Average);
        }

        /// <summary>
        /// average after rollover
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void RollingAverageAfterRollover()
        {
            RollingAverage<double> avg = new RollingAverage<double>(5);
            avg.AddSample(1);
            avg.AddSample(2);
            avg.AddSample(3);
            avg.AddSample(4);
            avg.AddSample(5);
            avg.AddSample(6);
            Assert.AreEqual(4, avg.Average);
        }

        /// <summary>
        /// correct sample counting
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void RollingAverageSampleCount()
        {
            RollingAverage<int> avg = new RollingAverage<int>(25);

            for (int i = 0; i < 55; i++)
            {
                avg.AddSample(i);
                Assert.AreEqual(Math.Min(i + 1, 25), avg.SampleCount);
            }
        }
    }
}
